<?php

/**
 * 数据分页类
 * 
 * @since 1.0.0 2013-4-13 sutroon
 * @example  
        $pager=new SoDataPager(request_numeric('page'));
        
        $dataset=$this->dm->select_pagingset($pager->pageid,$pager->pagesize,$pager->recordcount,'tgeneralarticle','CreatedTime desc','ID, Title, VisitCount, State, CreatedTime');        
        $pager->set_dataset($dataset,$pager->recordcount);
        $this->assign('pager',$pager->get_pager_text($this->set_url_param(array('page'=>'.pageid.'))));
 */
class SoDataPager {

    public $pagesize;
    public $pagevar;
    public $pageid;
    public $recordcount;
    public $pagecount;
    public $recordset;
    public $firstpage;
    public $lastpage;
    public $prevpage;
    public $nextpage;
    public $templatename;

    public function __construct($pageid, $pagesize = 20, $pagevar = 'page') {
        $this->pageid = $pageid;
        $this->pagesize = $pagesize;
        $this->pagevar = $pagevar;
        if ($this->pageid < 1)
            $this->pageid = 1;
        // 从全局配置文件读取模板名称
        $this->templatename=defined('TEMPLATE_NAME')?strtolower(TEMPLATE_NAME):'default';
    }

    /**
     * 设置并执行分页查询 2013-5-23 by sutroon
     * 执行数据库分页
     * @example path description
     */
    function set_dataset($dataset, $recordcount) {
        if(!$recordcount) $recordcount=0;
        $this->recordset = $dataset;
        $this->recordcount = $recordcount;
        $this->pagecount = ceil($this->recordcount / $this->pagesize);
        $this->paging();
    }

    /**
     * 设置并执行分页统计 2013-5-24 by sutroon
     * 只是简单地根据记录数量执行分页，不参与任何sql语句的操作
     * @example set_record_count(100);
     */
    function set_record_count($recordcount) {
        if(!$recordcount) $recordcount=0;
        $this->recordcount = $recordcount;
        $this->pagecount = ceil($recordcount / $this->pagesize);
        $this->paging();
    }

    private function paging() {
        $this->firstpage = 1;
        $this->lastpage = $this->pagecount;
        $this->prevpage = $this->pageid - 1;
        $this->nextpage = $this->pageid + 1;
        if ($this->prevpage < 1)
            $this->prevpage = 1;
        if ($this->nextpage > $this->pagecount)
            $this->nextpage = $this->pagecount;
    }

    /**
     * 获取分页条码字符串
     * @param type $url 带有分页占位符的地址,如 ?cid=5&page=.page.&sort=time
     * @param type $num 分页数字列表数量,如 上一页 1 2 3 4 5 6 下一页
     * @param type $showtip 是否显示统计信息
     * @return string 分页条码字符串
     * @since 1.0.0 2013-9-30
     * @example get_pager_text('?cid=5&page=.pageid.&sort=time');
     */
    public function get_pager_text($url, $num = 5, $showtip = true, $var = '.pageid.') {
         switch(strtolower($this->templatename)){
             case 'bootstrap3':
                 return $this->get_pager_text_bootstrap3($url, $num, $showtip, $var);
             case 'easyui':
                 return $this->get_pager_text_earyui();
             default:
                 return $this->get_pager_text_default($url, $num, $showtip, $var);
         }
    }
    
    /**
     * 获取符合Bootstrap规范的分页条
     * @param type $url
     * @param type $num
     * @param type $showtip
     * @param type $var
     * @return string
     * @since 1.0.0 2013-12-5 by sutroon
     */
    private function get_pager_text_bootstrap3($url, $num = 5, $showtip = true, $var = '.pageid.'){
        $str = '<ul class="pagination pagination-sm">';
        $str.='<li><a href="' . str_replace($var, 1, $url) . '">&laquo;</a></li>';
        $nums = $this->get_number_list($num);
        foreach ($nums as $i) {
            $str.='<li' . ($this->pageid == $i ? ' class="active"' : '') . '><a href="' . str_replace($var, $i, $url) . '">' . $i . '</a></li>';
        }
        $str.='<li><a href="' . str_replace($var, $this->pagecount, $url) . '">&raquo;</a></li>';
        if ($showtip)
            $str.='<li><span>页次:' . $this->pageid . '/' . $this->pagecount . ', 数量:' . $this->recordcount . '/' . $this->pagesize . '</span></li>';
        $str.='</ul>';
        return $str;
    }
    
    /**
     * 
     * @return string
     * @since 1.0.0 2014-1-19 by sutroon
     */
    private function get_pager_text_earyui(){    
        $str='<div class="easyui-pagination" style="border:1px solid #ddd;" data-options="
                total:'.$this->recordcount.',pageNumber:'.$this->pageid.',pageSize:'.$this->pagesize.',
                layout:[\'sep\',\'first\',\'prev\',\'links\',\'next\',\'last\',\'sep\',\'manual\'],
                onSelectPage:function (pageNumber, pageSize) { location.href=jQuery.query.set(\'page\', pageNumber).toString();},
                "></div>';
        return $str;
    }
    
    /**
     * 获取默认风格的分页条
     * @param type $url
     * @param type $num
     * @param type $showtip
     * @param type $var
     * @return string
     * @since 1.0.0 2013-12-5 by sutroon
     */
    private function get_pager_text_default($url, $num = 5, $showtip = true, $var = '.pageid.'){
        $str = '<a href="' . str_replace($var, 1, $url) . '">&laquo;</a>';
        $nums = $this->get_number_list($num);
        foreach ($nums as $i) {
            $str.='<a href="' . str_replace($var, $i, $url) . '">' . $i . '</a>';
        }
        $str.='<a href="' . str_replace($var, $this->pagecount, $url) . '">&raquo;</a>';
        if ($showtip)
            $str.='<span>页次:' . $this->pageid . '/' . $this->pagecount . ', 数量:' . $this->recordcount . '/' . $this->pagesize . '</span>';
        return $str;
    }
    

    /**
     * 获取分页数字列表
     * @param type $num
     * @return type
     */
    function get_number_list($num) {
        $div = ceil($num / 2);
        $start = $this->pageid - $div;
        $end = $this->pageid + $div;
        if ($start < 1)            $start = 1;
        if ($end > $this->pagecount)            $end = $this->pagecount;
        return $this->pagecount==0?array(1):range($start, $end);
    }

}

?>